Skip to content

Fix topic inheritance for translated documents #6633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

smithellis
Copy link
Contributor

This commit addresses issues with topic handling for translated documents:

  1. Adds signal handler to automatically propagate topic changes from parent documents to their translations, ensuring database consistency

  2. Improves query filtering in facets.py to include translated documents based on their parent's topics, providing correct results even before database synchronization occurs

These changes ensure that topics are consistently applied across all translations of a document, both at the database level and in query results.

This commit addresses issues with topic handling for translated documents:

1. Adds signal handler to automatically propagate topic changes from parent
   documents to their translations, ensuring database consistency

2. Improves query filtering in facets.py to include translated documents
   based on their parent's topics, providing correct results even before
   database synchronization occurs

These changes ensure that topics are consistently applied across all
translations of a document, both at the database level and in query results.
@smithellis smithellis force-pushed the 2183-updating-parent-topic-is-not-reflected-on-old-topic-listing branch from 24760fc to 4fb3cb1 Compare April 21, 2025 17:33
@smithellis
Copy link
Contributor Author

I've modified this to just fix the case where the translated document isn't being displayed in the right list. This no longer updates translated document topics based on parent, or listens for signals.

We may want to consider a solution for translation topics that doesn't keep bad data in the database - possibly just setting new translation document topics to null when they are created.

@smithellis smithellis marked this pull request as ready for review April 21, 2025 19:53
Copy link
Contributor

@escattone escattone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Two things:

@@ -112,7 +112,9 @@ def _documents_for(user, locale, topics=None, products=None):
if topics:
topic_ids = [t.id for t in topics]
# we need to filter against parent topics for localized articles
qs = qs.filter(Q(topics__in=topic_ids) | Q(parent__topics__in=topic_ids))
qs = qs.filter(
Q(topics__in=topic_ids) | Q(parent__isnull=False, parent__topics__in=topic_ids)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should also add the parent check to the first Q clause:

Q(parent__isnull=True, topics__in=topic_ids) | ...

Copy link
Contributor

@escattone escattone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+wc

See my comment below about avoiding the use of sets because we want to test the ordering of _documents_for().

@@ -267,28 +268,28 @@ def test_documents_for(self):
docs = _documents_for(
self.anonymous, locale="de", topics=[self.general_d, self.bookmarks_d]
)
self.assertEqual([d["id"] for d in docs], [self.doc1_localized.id])
self.assertEqual({d["id"] for d in docs}, {self.doc1_localized.id})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changing? I suppose due to a change in ordering where two or more docs in the result ranked the same in terms of order, and are swapping positions from run to run and causing flaky results? If so, you can tweak the display_order to ensure that those docs are ordered consistently.

Since _documents_for returns document dicts in a specific order, we should continue using lists instead of sets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - sorry, I thought these were just membership tests. I'll revise.


with self.subTest("documents_for-general_bookmarks_sync_localized-user1"):
docs = _documents_for(
self.user1, locale="de", topics=[self.general_d, self.bookmarks_d]
)
self.assertEqual([d["id"] for d in docs], [self.doc1_localized.id])
self.assertEqual({d["id"] for d in docs}, {self.doc1_localized.id})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


with self.subTest("documents_for-general_bookmarks_sync_localized-user2"):
docs = _documents_for(
self.user2, locale="de", topics=[self.general_d, self.bookmarks_d]
)
self.assertEqual(
[d["id"] for d in docs], [self.doc1_localized.id, self.doc8_localized.id]
{d["id"] for d in docs}, {self.doc1_localized.id, self.doc8_localized.id}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

)

with self.subTest("documents_for-general_bookmarks_sync_localized-staff"):
docs = _documents_for(
self.staff, locale="de", topics=[self.general_d, self.bookmarks_d]
)
self.assertEqual(
[d["id"] for d in docs], [self.doc1_localized.id, self.doc8_localized.id]
{d["id"] for d in docs}, {self.doc1_localized.id, self.doc8_localized.id}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@smithellis smithellis merged commit d7ba74f into mozilla:main Apr 25, 2025
2 checks passed
@smithellis smithellis deleted the 2183-updating-parent-topic-is-not-reflected-on-old-topic-listing branch April 25, 2025 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants